<?php

namespace App\Http\Controllers\Api\V1\Frontend\Report;

use Illuminate\Http\Request;
use App\Services\ReportService;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;
use Illuminate\Validation\ValidationException;

class IndexController extends Controller
{
    /**
     * 报工展示
     * @param Request $request
     * @param ReportService $reportService
     * @return mixed
     * @throws ValidationException
     */
    public function index(Request $request,ReportService $reportService)
    {
        Validator::make($request->all(),[
            'type'   => 'nullable|int|in:1,2,3',
            'search' => 'nullable|string',
            'page'   => 'nullable|int',
            'date'   => 'nullable|date_format:Y-m-d'
        ])->validate();

        //默认展示我的报工
        if (!$request->filled('type')) {
            $request->offsetSet('type',1);
        }

        $data = $reportService->index($request->input('type'),$request->only('search','page','date'));

        if ($data['code'] == 200){
            return $this->successful($data['data'],'success');
        }

        return $this->failed($data['message']);
    }

    /**
     * 报工审批
     * @param Request $request
     * @param ReportService $reportService
     * @return mixed|void
     * @throws ValidationException
     */
    public function approval(Request $request,ReportService $reportService)
    {
        Validator::make($request->all(),[
            'id'        => 'required|array',
            'comment'   => 'nullable|string|max:200',
            'status'    => 'required|int|in:0,1'
        ],[
            'id.required'       => '报工数据识别失败，请退出后重新审批',
            'status.required'   => '请选择审批处理方式'
        ])->validate();

        if ($request->input('status') == 0){
            if (!$request->filled('comment')){
                return $this->failed('驳回，请填写驳回原因');
            }
        }

        $data = $reportService->approval($request->only('id','comment','status'));

        if ($data['code'] == 200){
            return $this->successful($data['data'],'审核成功');
        }

        return $this->failed($data['message']);
    }

    /**
     * 审批流程
     * @param Request $request
     * @param ReportService $reportService
     * @return mixed
     * @throws ValidationException
     */
    public function approvalProcess(Request $request,ReportService $reportService)
    {
        Validator::make($request->all(),[
            'id' => 'required|string',
        ])->validate();

        $data = $reportService->getApprovalProcess($request->input('id'));

        if ($data['code'] == 200){
            return $this->successful($data['data'],'success');
        }

        return $this->failed($data['message']);
    }

    /**
     * 判断报工是否已填报
     * @param Request $request
     * @param ReportService $reportService
     * @return mixed
     * @throws ValidationException
     */
    public function getReportStatus(Request $request,ReportService $reportService)
    {
        Validator::make($request->all(),[
            'date' => 'nullable|date_format:Y-m-d',
        ])->validate();

        if (!$request->filled('date')) {
            $request->offsetSet('date',date('Y-m-d',time()));
        }

        $data = $reportService->getReportStatus($request->input('date'));

        if ($data['code'] == 200){
            return $this->successful($data['data'],'success');
        }

        return $this->failed($data['message'],422);
    }

    /**
     * 报工填报
     * 计划内报工和计划外报工填报
     * @param Request $request
     * @param ReportService $reportService
     * @return mixed
     * @throws ValidationException
     */
    public function report(Request $request,ReportService $reportService)
    {
        Validator::make($request->all(),[
            'report_type' => 'required|int|in:1,2'
        ],[
            'report_type.required'  => '请选择填报类型',
        ])->validate();

        if (!$request->filled('date')) {
            $request->offsetSet('date',date('Y-m-d',time()));
        }

        if ($request->input('report_type') == 1) {
            Validator::make($request->all(),[
                'project_name'           => 'required|string',
                'project_id'             => 'required|int',
                'task_name'              => 'required|string',
                'task_id'                => 'required|string',
                'category_id'            => 'required|int',
                'complete_percent'       => 'nullable|numeric|min:0|max:100',
                'work_type'              => 'required|int',
                'plan_hours'             => 'nullable|numeric',
                'used_hours'             => 'nullable|numeric',
                'standard_hours'         => [
                    'required',
                    'numeric',
                    Rule::notIn([0]),
                    'min:0',
                    'max:8'
                ], //'nullable|numeric',
                'common_overtime_hours'  => 'nullable|numeric',
                'holiday_hours'          => 'nullable|numeric',
                'other_hours'            => 'nullable|numeric',
                'work_report'            => 'nullable|string',
                'comment'                => 'nullable|string|max:200',
                'date'                   => 'required|date_format:Y-m-d'
            ],[
                'project_name.required'         => '请选择 项目',
                'project_id.required'           => '请选择 项目',
                'task_name.required'            => '请选择 任务',
                'task_id.required'              => '请选择 任务',
                'category_id.required'          => '任务类型 获取失败，请重新选择任务',
                'work_type.required'            => '请选择 工作类型',
                'date.required'                 => '报工日期 识别失败，请退出后重新报工',
                'standard_hours.required'       => '正常工时 应填大于0且小于或等于8的数字',
                'standard_hours.numeric'        => '正常工时 应填大于0且小于或等于8的数字',
                'standard_hours.not_in'         => '正常工时 应填大于0且小于或等于8的数字',
                'standard_hours.min'            => '正常工时 应填大于0且小于或等于8的数字',
                'standard_hours.max'            => '正常工时 应填大于0且小于或等于8的数字',
                'common_overtime_hours.numeric' => '普通加班工时 必需填写数字',
                'holiday_hours.numeric'         => '法定节假日加班工时 必需填写数字',
                'other_hours.numeric'           => '出差工时 必需填写数字',
                'complete_percent.numeric'      => '任务进展 应填0~100的数字',
                'complete_percent.min'          => '任务进展 应填0~100的数字',
                'complete_percent.max'          => '任务进展 应填0~100的数字'
            ])->validate();
        } else {
            Validator::make($request->all(),[
                'title'                  => 'required|string',
                'project_name'           => 'required|string',
                'project_id'             => 'required|int',
                'complete_percent'       => 'nullable|numeric|min:0|max:100',
                'task_type'              => 'required|int',
                'work_type'              => 'required|int',
                'standard_hours'         => [
                    'required',
                    'numeric',
                    Rule::notIn([0]),
                    'min:0',
                    'max:8'
                ], //'nullable|numeric',
                'common_overtime_hours'  => 'nullable|numeric',
                'holiday_hours'          => 'nullable|numeric',
                'other_hours'            => 'nullable|numeric',
                'comment'                => 'nullable|string|max:200',
                'work_report'            => 'nullable|string',
                'date'                   => 'required|date_format:Y-m-d'
            ],[
                'title.required'                => '请输入 标题',
                'project_name.required'         => '请选择 项目',
                'project_id.required'           => '请选择 项目',
                'task_type.required'            => '请选择 任务类型',
                'work_type.required'            => '请选择 工作类型',
                'date.required'                 => '报工日期 识别失败，请退出后重新报工',
                'standard_hours.required'       => '正常工时 应填大于0且小于或等于8的数字',
                'standard_hours.numeric'        => '正常工时 应填大于0且小于或等于8的数字',
                'standard_hours.not_in'         => '正常工时 应填大于0且小于或等于8的数字',
                'standard_hours.min'            => '正常工时 应填大于0且小于或等于8的数字',
                'standard_hours.max'            => '正常工时 应填大于0且小于或等于8的数字',
                'common_overtime_hours.numeric' => '普通加班工时 必需填写数字',
                'holiday_hours.numeric'         => '法定节假日加班工时 必需填写数字',
                'other_hours.numeric'           => '出差工时 必需填写数字',
                'date.date_format'              => '报工日期 格式不正确',
                'complete_percent.numeric'      => '任务进展 应填0~100的数字',
                'complete_percent.min'          => '任务进展 应填0~100的数字',
                'complete_percent.max'          => '任务进展 应填0~100的数字'
            ])->validate();
        }
//        return $this->failed($reportService->getReportHour($request->get('date'), 1, '006518006a16482892533d1e657e3ceba'));
        $data = $reportService->createReport($request->all());

        if ($data['code'] == 200){
            return $this->successful($data['data'],$data['message']);
        }

        return $this->failed($data['message']);
    }

    /**
     * 获取报工详情
     * @param Request $request
     * @param ReportService $reportService
     * @return mixed
     */
    public function reportDetails(Request $request,ReportService $reportService)
    {
        Validator::make($request->all(),[
            'report_id' => 'required|string'
        ],[
            'report_id.required'    => '报工数据识别失败，请退出后重新操作',
        ]);

        $data = $reportService->getReportDetails($request->input('report_id'));

        if ($data['code'] == 200){
            return $this->successful($data['data'],'success');
        }

        return $this->failed($data['message']);
    }

    /**
     * 编辑报工
     * @param Request $request
     * @param ReportService $reportService
     * @return mixed
     * @throws ValidationException
     */
    public function updateReport(Request $request,ReportService $reportService)
    {
        Validator::make($request->all(),[
            'report_id' => 'required|string'
        ],[
            'report_id.required'    => '报工数据识别失败，请退出后重新操作',
        ])->validate();

        $type = $request->filled('report_type')
            ?$request->input('report_type')
            :$reportService->getReportType($request->input('report_id'));

        if (is_array($type)) {
            return $this->failed($type['message']);
        }

        if ($type == 1) {
            Validator::make($request->all(),[
                'project_name'           => 'required|string',
                'project_id'             => 'required|int',
                'task_name'              => 'required|string',
                'task_id'                => 'required|string',
                'category_id'            => 'required|int',
                'work_type'              => 'required|int',
                'complete_percent'       => 'nullable|numeric|min:0|max:100',
                'plan_hours'             => 'nullable|numeric',
                'used_hours'             => 'nullable|numeric',
                'standard_hours'         => [
                    'required',
                    'numeric',
                    Rule::notIn([0]),
                    'min:0',
                    'max:8'
                ], //'nullable|numeric',
                'common_overtime_hours'  => 'nullable|numeric',
                'holiday_hours'          => 'nullable|numeric',
                'other_hours'            => 'nullable|numeric',
                'comment'                => 'nullable|string|max:200',
                'work_report'            => 'nullable|string'
            ],[
                'work_type.required'            => '请选择 工作类型',
                'project_name.required'         => '请选择 项目',
                'project_id.required'           => '请选择 项目',
                'task_name.required'            => '请选择 任务',
                'task_id.required'              => '请选择 任务',
                'category_id.required'          => '任务类型 获取失败，请重新选择任务',
                'standard_hours.required'       => '正常工时 应填大于0且小于或等于8的数字',
                'standard_hours.numeric'        => '正常工时 应填大于0且小于或等于8的数字',
                'standard_hours.not_in'         => '正常工时 应填大于0且小于或等于8的数字',
                'standard_hours.min'            => '正常工时 应填大于0且小于或等于8的数字',
                'standard_hours.max'            => '正常工时 应填大于0且小于或等于8的数字',
                'common_overtime_hours.numeric' => '普通加班工时 必需填写数字',
                'holiday_hours.numeric'         => '法定节假日加班工时 必需填写数字',
                'other_hours.numeric'           => '出差工时 必需填写数字',
                'complete_percent.numeric'      => '任务进展 应填0~100的数字',
                'complete_percent.min'          => '任务进展 应填0~100的数字',
                'complete_percent.max'          => '任务进展 应填0~100的数字'
            ])->validate();
        } else {
            Validator::make($request->all(),[
                'title'                  => 'required|string',
                'project_name'           => 'required|string',
                'project_id'             => 'required|int',
                'complete_percent'       => 'nullable|numeric|min:0|max:100',
                'task_type'              => 'required|int',
                'work_type'              => 'required|int',
                'standard_hours'         => [
                    'required',
                    'numeric',
                    Rule::notIn([0]),
                    'min:0',
                    'max:8'
                ], //'nullable|numeric',
                'common_overtime_hours'  => 'nullable|numeric',
                'holiday_hours'          => 'nullable|numeric',
                'other_hours'            => 'nullable|numeric',
                'comment'                => 'nullable|string|max:200',
                'work_report'            => 'nullable|string'
            ],[
                'title.required'                => '请输入 标题',
                'project_name.required'         => '请选择 项目',
                'project_id.required'           => '请选择 项目',
                'task_type.required'            => '请选择 任务类型',
                'work_type.required'            => '请选择 工作类型',
                'standard_hours.required'       => '正常工时 应填大于0且小于或等于8的数字',
                'standard_hours.numeric'        => '正常工时 应填大于0且小于或等于8的数字',
                'standard_hours.not_in'         => '正常工时 应填大于0且小于或等于8的数字',
                'standard_hours.min'            => '正常工时 应填大于0且小于或等于8的数字',
                'standard_hours.max'            => '正常工时 应填大于0且小于或等于8的数字',
                'common_overtime_hours.numeric' => '普通加班工时 必需填写数字',
                'holiday_hours.numeric'         => '法定节假日加班工时 必需填写数字',
                'other_hours.numeric'           => '出差工时 必需填写数字',
                'complete_percent.numeric'      => '任务进展 应填0~100的数字',
                'complete_percent.min'          => '任务进展 应填0~100的数字',
                'complete_percent.max'          => '任务进展 应填0~100的数字'
            ])->validate();
        }

        $data = $reportService->updateReport($request->all());

        if ($data['code'] == 200){
            return $this->successful($data['data'],'编辑成功');
        }

        return $this->failed($data['message']);
    }

    /**
     * 删除报工
     * @param Request $request
     * @param ReportService $reportService
     * @return mixed
     */
    public function destroyReport(Request $request, ReportService $reportService)
    {
        Validator::make($request->all(),[
            'report_id' => 'required|string'
        ],[
            'report_id.required'    => '报工数据识别失败，请退出后重新操作',
        ]);

        $data = $reportService->destroyReportDetails($request->input('report_id'));

        if ($data['code'] == 200){
            return $this->successful($data['data'],'删除成功');
        }

        return $this->failed($data['message']);
    }

    /**
     * 根据项目获取到任务名称
     * @param Request $request
     * @param ReportService $reportService
     * @return mixed
     * @throws ValidationException
     */
    public function task(Request $request,ReportService $reportService)
    {
        Validator::make($request->all(),[
            'project_id' => 'required|string',
        ])->validate();

        $data = $reportService->getTask($request->input('project_id'));

        if ($data['code'] == 200){
            return $this->successful($data['data'],'success');
        }

        return $this->failed($data['message']);
    }

    /**
     * 获取项目或则任务
     * @param Request $request
     * @param ReportService $reportService
     * @return mixed
     * @throws ValidationException
     */
    public function project(Request $request,ReportService $reportService)
    {
        Validator::make($request->all(),[
            'search' => 'nullable|string',
        ])->validate();

        $data = $reportService->getProject($request->input('search',''));

        if ($data['code'] == 200){
            return $this->successful($data['data'],'success');
        }

        return $this->failed($data['message']);
    }

    /**
     * 获取到工作类型
     * @param ReportService $reportService
     * @return mixed
     */
    public function workType(ReportService $reportService)
    {
        $data = $reportService->getWorkType();

        if ($data['code'] == 200){
            return $this->successful($data['data'],'success');
        }

        return $this->failed($data['message']);
    }

    /**
     * 获取到任务类型
     * @param ReportService $reportService
     * @return mixed
     */
    public function taskType(ReportService $reportService)
    {
        $data = $reportService->getTaskType();

        if ($data['code'] == 200){
            return $this->successful($data['data'],'success');
        }

        return $this->failed($data['message']);
    }
}
